<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品类目列表</title>
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/css/layui.css">
    <script src="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/layui.js" charset="utf-8"></script>
</head>

<body>
    <div style="margin: 15px;padding: 15px;background-color: #fff;">
        <div style="text-align: right;">
            {if $pid>0}
            <button class="layui-btn layui-btn-xs layui-btn-primary" onclick="back({$pid})">返回</button>
            {/if}
            <button class="layui-btn layui-btn-xs" onclick="add({$pid})">添加</button>
        </div>
        <table class="layui-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>类目名称</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                {volist name="cates" id="cate"}
                <tr>
                    <td>{$cate['id']}</td>
                    <td>{$cate['title']}</td>
                    <td>
                        <button class="layui-btn layui-btn-xs layui-btn-normal"
                            onclick="child({$cate['id']})">子类目</button>
                        <button class="layui-btn layui-btn-xs" onclick="edit({$cate['id']})">编辑</button>
                        <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="del({$cate['id']})">删除</button>
                    </td>
                </tr>
                {/volist}
            </tbody>
        </table>
    </div>

    <script>
        var $ = layui.jquery;

        //添加
        function add(pid) {
            layer.open({
                title: '添加类目',
                type: 2,
                maxmin: true,
                shadeClose: true,
                area: ['480px', '450px'],
                content: '/admins/products/add_cates?pid=' + pid,
            });
        }

        //子类目
        function child(id) {
            window.location.href = '/admins/products/cates?pid=' + id;
        }

        //编辑
        function edit(id) {
            layer.open({
                title: '编辑类目',
                type: 2,
                maxmin: true,
                shadeClose: true,
                area: ['480px', '450px'],
                content: '/admins/products/edit_cate?id=' + id,
            });
        }

        //删除
        function del(id) {
            layer.confirm('确定删除吗？', { btn: ['确定', '取消'], icon: 3 }, function () {
                $.post('/admins/products/delete_cates', { id }, function (res) {
                    if (res.code) return layer.alert(res.msg, { icon: 2 });
                    layer.alert(res.msg, { icon: 1 });
                    setTimeout(() => window.location.reload(), 1000);
                }, 'json');
            });
        }

        //返回
        function back(pid) {
            window.location.href = '/admins/products/cates?ppid=' + pid
        }
    </script>
</body>

</html>